package com.kawins.spring.valid.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 身份证工具
 *
 * @author rankai
 *         createTime 2017-04-2017/4/26 14:12
 */
public class IDCardUtil {

    private static final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    private static final int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
    private static final int[] ai = new int[18];

    /**
     * 验证身份证格式是正确
     *
     * @param idCard 身份证号
     * @return 格式是否正确
     */
    public static boolean verify(String idCard) {
        if (idCard == null) {
            return false;
        }
        idCard = idCard.trim();
        if (idCard.length() == 15) {
            idCard = upToEighteen(idCard);
        }
        if (idCard.length() != 18) {
            return false;
        }
        String verify = idCard.substring(17, 18);
        if (verify.equals(getVerify(idCard))) {
            return true;
        }
        return false;
    }

    /**
     * 获得生日
     *
     * @param idcard
     * @return
     */
    public static Date getBirthdayByIDcard(String idcard) {
        if (verify(idcard)) {
            String birthday = idcard.substring(6, 14);
            try {
                return new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获得年龄
     *
     * @param idCard
     * @return
     */
    public static int getAgeByIDcard(String idCard) {
        if (verify(idCard)) {
            Calendar _cal = Calendar.getInstance();
            Calendar today = Calendar.getInstance();
            _cal.set(Integer.parseInt(idCard.substring(6, 10)), Integer.parseInt(idCard.substring(10, 12)),
                    Integer.parseInt(idCard.substring(12, 14)));
            return getYearDiff(today, _cal);
        }
        return 0;
    }

    /**
     * 获得性别
     *
     * @param idCard
     * @return
     */
    public static SexEnum getSexByIDCard(String idCard) {
        if (verify(idCard)) {
            String lastValue = idCard.substring(idCard.length() - 2, idCard.length() - 1);
            int sex;
            sex = Integer.parseInt(lastValue) % 2;
            return sex == 0 ? SexEnum.WOMAN : SexEnum.MAN;
        }
        return SexEnum.MAN;
    }

    private static int getYearDiff(Calendar cal, Calendar _cal) {
        int m = (cal.get(cal.MONTH)) - (_cal.get(_cal.MONTH));
        int y = (cal.get(cal.YEAR)) - (_cal.get(_cal.YEAR));
        return (y * 12 + m) / 12;
    }

    private static String getVerify(String idCard) {
        int remaining = 0;
        if (idCard.length() == 18) {
            idCard = idCard.substring(0, 17);
        }
        if (idCard.length() == 17) {
            int sum = 0;
            for (int i = 0; i < 17; i++) {
                String k = idCard.substring(i, i + 1);
                ai[i] = Integer.parseInt(k);
            }
            for (int i = 0; i < 17; i++) {
                sum = sum + wi[i] * ai[i];
            }
            remaining = sum % 11;
        }
        return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
    }

    private static String upToEighteen(String idCard) {
        String eightCardId = idCard.substring(0, 6);
        eightCardId = eightCardId + "19";
        eightCardId = eightCardId + idCard.substring(6, 15);
        eightCardId = eightCardId + getVerify(eightCardId);
        return eightCardId;
    }

    public enum SexEnum {

        MAN(1, "男"),
        WOMAN(2, "女");

        private int code;
        private String sexSrt;

        SexEnum(int code, String sexSrt) {
            this.code = code;
            this.sexSrt = sexSrt;
        }

        public int getCode() {
            return code;
        }

        public String getSexSrt() {
            return sexSrt;
        }
    }

    public static void main(String[] args) {
        System.out.println(verify("350784645555441"));
    }
}

